﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;

namespace JuCloudy.Data.Nhibernate.App.Toursim
{
    public class ProductContainer : JuCloudy.Service.Intreface.jService
    {
        private static global::NHibernate.Cfg.Configuration mCfg = null;
        private static global::NHibernate.ISessionFactory SessionFactory = null;

        public ProductContainer()
        {
            mCfg = new NHibernate.Cfg.Configuration().Configure(
               System.Web.HttpContext.Current.Server.MapPath(
                    System.Configuration.ConfigurationManager.AppSettings["AppToursimNhCfg"]
                    ));

            SessionFactory = null;

            mCfg.AddClass(typeof(JuCloudy.Data.Entity.AppToursimProduct));
            SessionFactory = mCfg.BuildSessionFactory();
        }
        public string Insert(string JsonOfEntity)
        {
            var entity = JsonOfEntity.Deserialize(typeof(JuCloudy.Data.Entity.AppToursimProduct)) as JuCloudy.Data.Entity.AppToursimProduct;
            entity.CreateDate = DateTime.Now;
            using (NHibernate.ISession session = SessionFactory.OpenSession())
            {
                var query = from c in session.Query<JuCloudy.Data.Entity.AppToursimProduct>()
                                where c.Departure == entity.Departure 
                                && c.Destination == entity.Destination 
                                && c.Durain==entity.Durain 
                                && c.GoTraf==entity.GoTraf 
                                && c.PartnerId==entity.PartnerId 
                                && c.PlanType==entity.PlanType 
                                && c.ProductType == entity.ProductType 
                                && c.ReTraf==entity.ReTraf                                
                                select c;
                if (query != null && query.Count() > 0)
                {
                    return query.ToList()[0].Serialize();
                }
                else
                {
                    using (NHibernate.ITransaction vTransaction = session.BeginTransaction())
                    {
                        try
                        {
                            session.SaveOrUpdate(entity);
                            vTransaction.Commit();
                            vTransaction.Dispose();
                            session.Flush();
                            session.Close();
                            session.Dispose();
                            return entity.Serialize();
                        }
                        catch (Exception ex)
                        {
                            vTransaction.Rollback();
                            vTransaction.Dispose();
                            session.Flush();
                            session.Close();
                            session.Dispose();
                            throw ex;
                        }
                    }
                }
            }

        }

        public string Delete(string JsonOfEntity)
        {
            var entity = JsonOfEntity.Deserialize(typeof(JuCloudy.Data.Entity.AppToursimProduct)) as JuCloudy.Data.Entity.AppToursimProduct;
            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Delete(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.AppToursimProduct>().ToList();
                        vTransaction.Dispose(); session.Flush();
                        session.Close();
                        session.Dispose();
                        return query.Serialize();
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }

        public string GetEntity(string Query)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                int res = 0;
                if (int.TryParse(Query, out res))
                {
                    var query = session.Query<JuCloudy.Data.Entity.AppToursimProduct>().ToList();
                    session.Flush();
                    session.Close();
                    session.Dispose();
                    return query.Serialize();
                }
                else
                {
                    var entity = Query.Deserialize(typeof(JuCloudy.Data.Entity.AppToursimProduct)) as JuCloudy.Data.Entity.AppToursimProduct;
                    var query = from c in session.Query<JuCloudy.Data.Entity.AppToursimProduct>()
                                where c.Departure == entity.Departure 
                                && c.Destination == entity.Destination 
                                && c.Durain==entity.Durain 
                                && c.GoTraf==entity.GoTraf 
                                && c.PartnerId==entity.PartnerId 
                                && c.PlanType==entity.PlanType 
                                && c.ProductType == entity.ProductType 
                                && c.ReTraf==entity.ReTraf                                
                                select c;
                    if (query != null && query.Count() > 0)
                    {

                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return query.Serialize();
                    }
                    else
                    {
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return String.Empty;
                    }
                }
            }
        }

        public string GetEntities()
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                var query = session.Query<JuCloudy.Data.Entity.AppToursimProduct>().ToList();
                session.Flush();
                session.Close();
                session.Dispose();
                return query.Serialize();
            }
        }
    }
}
